﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Drawing;
using System.Runtime.InteropServices;

using DNA;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Application = Autodesk.AutoCAD.ApplicationServices.Application;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using TJADRICD.CollaborativeDesignPlugin;
namespace TJADRICD.CollaborativeDesignPlugin.Function
{
    public static class Function_Verify
    {
        public static Int32 TJAD_SIGN_ReadName(String file, StringBuilder name)
        {
            UInt32 key1 = 0x32f86773;
            UInt32 key2 = 0x24e27931;
            UInt32 key3 = 0x81eefed5;
            UInt32 key4 = 0x1ff77e95;

            FileInfo fi = new FileInfo(file);
            if (!fi.Exists)
                return -1;

            Byte[] bytebuff1 = new Byte[128];
            Byte[] bytebuff2 = new Byte[128];
            Byte[] bytebuff = new Byte[256];

            FileStream ifile = File.Open(file, FileMode.Open);
            ifile.Read(bytebuff, 0, 256);
            ifile.Close();

            for (int i = 0; i < 128; i++)
            {
                bytebuff1[i] = bytebuff[i];
                bytebuff2[i] = bytebuff[i + 128];
            }

            Extern.AES_Decrypt_Data_I(bytebuff1, key4, key2, key3, key1, 128);

            UInt32 A = 0;
            UInt32 B = 0;
            UInt32 C = 0;
            UInt32 D = 0;

            Extern.MD5_Data_I(bytebuff1, ref A, ref B, ref C, ref D, 128);

            Extern.AES_Decrypt_Data_I(bytebuff2, A, B, C, D, 128);

            name.Append(System.Text.Encoding.Default.GetString(bytebuff2));
            for (int i = 0; i < name.Length; i++)
            {
                if (name[i] == '\0')
                {
                    name.Remove(i, name.Length - i);
                    break;
                }
            }
            return 0;
        }
    }
}
